class Controller {
    constructor(systemService, uiNotification, $state, $scope, uiAlert, $rootScope, $cookies) {
        "ngInject"
        this.notify    = uiNotification;
        this.service   = systemService;

        this.state     = $state;
        this.scope     = $scope;
        this.cookies   = $cookies;
        this.rootScope = $rootScope;
        this.uiAlert   = uiAlert;
        this.data = {}
    }

    getPwdRule() {
        if (!this.data.username) return;
        if (this.pLoading) return;
        this.pLoading = true;
        this.service.getPwdRule('user', { username: this.data.username }).then(res => {
            this.pwdRule = res.data;
            this.pLoading = false;
        })
    }

    resetPwd () {
        if (this.sLoading) return;
        this.sLoading = true;
        this.service.resetPwd(this.data).then( () => {
            this.uiAlert.success('修改成功!', {backdrop: 'static', cancelText: false}).then(() => {
                const win = angular.element(window)[0];
                win.opener = null;
                win.top.close()
            })

        }).catch((data) => {
            this.notify.error({
                'title'  : '提示',
                'message': '操作失败. ' + (data.resultMsg ? data.resultMsg : '')
            });
        }).finally(() => {
            // 隐藏 loading
            this.sLoading = false
        });
    }

    checkValid (field) {
        console.log(field)
        if ( field.$pristine ) {
            return field.$pristine = false;
        }
        if ( field.$name == 'username' ) {
            this.getPwdRule()
        }
    }
}

export default Controller;